<?php
session_start();
$userID = $_SESSION['userID'];
//$userID = 5;

mysql_connect('localhost', 'root');
mysql_select_db('dots');

$query = "SELECT battleID, fieldID, autorID FROM battles WHERE (autorID=$userID OR guestID=$userID) AND finish=0";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

if ($row)
{
	$battleID = $row[0];
	$fieldID = $row[1];
	
	$_SESSION['battleID'] = $battleID;
	$_SESSION['fieldID'] = $fieldID;
	
	$file = fopen('../battles/log'.$battleID.'.txt', 'r');
	$line = fread($file, 1000);
	fclose($file);
	
	if (($line[5] == 1 && $row[2] == $userID) || ($line[5] == 0 && $row[2] != $userID))
	{
		print 'resp=no';
	}
	else
	{
		print 'resp=yes&'.$line;
	}
}
else
{
	$battleID = $_SESSION['battleID'];
	$fieldID = $_SESSION['fieldID'];
	
	$file = fopen('../battles/log'.$battleID.'.txt', 'r');
	$line = fread($file, 1000);
	fclose($file);
	print 'resp=yes&'.$line;
}
?>